/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-09-27 09:05:30
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-22 17:33:07
 * @FilePath: /react18-vite-admin/src/store/modules/tagsView.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { create } from 'zustand';

import type { TagsViewStore } from '@/typings/store/tagsView';

export const useTagsViewStore = create<TagsViewStore>((set, get) => {
    return {
        visitedViews: [],
        cachedViews: [],
        refresh: undefined,
        setRefresh(refresh) {
            set({
                refresh
            });
        },
        /**
         * 添加已访问视图到已访问视图列表中
         */
        addVisitedView(view) {
            const store = get();
            // 如果已经存在于已访问的视图列表中，则不再添加
            if (store.visitedViews.some((v) => v.path === view.path)) {
                return;
            }
            // 如果视图是固定的（affix），则在已访问的视图列表的开头添加
            if (view.affix) {
                set((state) => {
                    return {
                        visitedViews: [view, ...state.visitedViews]
                    };
                });
            } else {
                // 如果视图不是固定的，则在已访问的视图列表的末尾添加
                set((state) => {
                    return {
                        visitedViews: [...state.visitedViews, view]
                    };
                });
            }
        },
        /**
         * 添加缓存视图到缓存视图列表中
         */
        addCachedView(view) {
            const store = get();
            const viewName = view.name;
            // 如果缓存视图名称已经存在于缓存视图列表中，则不再添加
            if (store.cachedViews.includes(viewName)) {
                return;
            }

            // 如果视图需要缓存（keepAlive），则将其路由名称添加到缓存视图列表中
            if (view.keepAlive) {
                set((state) => {
                    return {
                        cachedViews: [...state.cachedViews, viewName]
                    };
                });
            }
        },
        /**
         * 从已访问视图列表中删除指定的视图
         */
        delVisitedView(view) {
            const visitedViews = [...get().visitedViews];
            for (const [i, v] of visitedViews.entries()) {
                // 找到与指定视图路径匹配的视图，在已访问视图列表中删除该视图
                if (v.path === view.path) {
                    visitedViews.splice(i, 1);
                    set({
                        visitedViews
                    });
                    break;
                }
            }
        },
        delCachedView(view) {
            const viewName = view.name;
            const cachedViews = [...get().cachedViews];
            const index = cachedViews.indexOf(viewName);
            if (index > -1) {
                cachedViews.splice(index, 1);
                set({
                    cachedViews
                });
            }
        },
        delOtherVisitedViews(view) {
            const visitedViews = [...get().visitedViews];
            set({
                visitedViews: visitedViews.filter((v) => {
                    return v?.affix || v.path === view.path;
                })
            });
        },
        delOtherCachedViews(view) {
            const viewName = view.name;
            let cachedViews = [...get().cachedViews];
            const index = cachedViews.indexOf(viewName);
            if (index > -1) {
                cachedViews = cachedViews.slice(index, index + 1);
            } else {
                // if index = -1, there is no cached tags
                cachedViews = [];
            }
            set({
                cachedViews
            });
        },
        updateVisitedView(view) {
            const visitedViews = [...get().visitedViews];
            for (let v of visitedViews) {
                if (v.path === view.path) {
                    v = Object.assign({}, v, view);
                    break;
                }
            }
            set({
                visitedViews
            });
        },
        addView(view) {
            get().addVisitedView(view);
            get().addCachedView(view);
        },
        delView(view) {
            get().delVisitedView(view);
            get().delCachedView(view);
        },
        delOtherViews(view) {
            get().delOtherVisitedViews(view);
            get().delOtherCachedViews(view);
        },
        delLeftViews(view) {
            const visitedViews = [...get().visitedViews];
            const currIndex = visitedViews.findIndex(
                (v) => v.path === view.path
            );
            if (currIndex === -1) {
                return;
            }
            const views = visitedViews.filter((item, index) => {
                if (index >= currIndex || item?.affix) {
                    return true;
                }
                return false;
            })
            set({
                visitedViews: views,
                cachedViews: views.filter(item => item.keepAlive).map(item => {
                    return item.name
                })
            });
        },
        delRightViews(view) {
            const visitedViews = [...get().visitedViews];
            const currIndex = visitedViews.findIndex(
                (v) => v.path === view.path
            );
            if (currIndex === -1) {
                return;
            }
            const views = visitedViews.filter((item, index) => {
                if (index <= currIndex || item?.affix) {
                    return true;
                }
            })
            set({
                visitedViews: views,
                cachedViews: views.filter(item => item.keepAlive).map(item => {
                    return item.name
                })
            });
        },
        delAllViews() {
            const visitedViews = [...get().visitedViews];
            const affixTags = visitedViews.filter((tag) => tag?.affix);
            set({
                visitedViews: affixTags,
                cachedViews: []
            });
            return affixTags;
        },
        delAllVisitedViews() {
            const visitedViews = [...get().visitedViews];
            const affixTags = visitedViews.filter((tag) => tag?.affix);
            set({
                visitedViews: affixTags
            });
        },
        delAllCachedViews() {
            set({
                cachedViews: []
            });
        }
    };
});
